package com.xhwl.logistics.open;

import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.dto.ResponseCommonPushKeTopDTO;
import com.xhwl.logistics.dto.push.CarInKeTopDTO;
import com.xhwl.logistics.dto.push.CarOutKeTopDTO;
import com.xhwl.logistics.dto.push.CarPayKeTopDTO;
import com.xhwl.logistics.service.business.VehicleAlarmBusinessService;
import com.xhwl.logistics.service.business.VehicleDataPushBusinessService;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.service.impl.redis.VehicleDataPushKeTopRedisServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleIntegratedConfigRedisServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleLogRedisServiceImpl;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Slf4j
//@RestController
public class KeTopDataPushController {

    @Autowired
    private VehicleLogRedisServiceImpl vehicleLogRedisService;

    @Autowired
    private VehicleDataPushKeTopRedisServiceImpl vehicleDataPushKeTopRedisService;

    @Autowired
    private VehicleIntegratedConfigRedisServiceImpl vehicleIntegratedConfigRedisService;

    @Autowired
    private QueryProjectService queryProjectService;

    @Autowired
    private VehicleDataPushBusinessService vehicleDataPushBusinessService;

    @Autowired
    private VehicleAlarmBusinessService vehicleAlarmBusinessService;

    @PostMapping("/PostCarInInfo")
    public ResponseCommonPushKeTopDTO carIn(@RequestBody CarInKeTopDTO carInKeTopDTO) {
        log.info("start: ke-top push data for car in");

        vehicleLogRedisService.log("access-record-in:" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), carInKeTopDTO);
        vehicleDataPushKeTopRedisService.insertAccessRecordForIn(carInKeTopDTO);

        Integer projectId = vehicleIntegratedConfigRedisService.queryProjectIdByParkId(String.valueOf(carInKeTopDTO.getParkId()));
        ProjectBO projectBO = queryProjectService.queryProject(projectId);

        vehicleAlarmBusinessService.sendAlarmForKeTopCarInExceptionPass(projectId, carInKeTopDTO.getPassType(), carInKeTopDTO.getImgInfo(),
                StringUtils.substringAfterLast(carInKeTopDTO.getEntryPlace(), "-"));
        vehicleAlarmBusinessService.sendAlarmForKeTopBlacklist(projectId, carInKeTopDTO.getPlateNo(), carInKeTopDTO.getImgInfo());

        vehicleDataPushBusinessService.keTopCarIn(carInKeTopDTO, projectBO);

        log.info("end: ke-top push data for car in");
        return ResponseCommonPushKeTopDTO.success();
    }

    @PostMapping("/PostCarOutInfo")
    public ResponseCommonPushKeTopDTO carOut(@RequestBody CarOutKeTopDTO carOutKeTopDTO) {
        log.info("start: ke-top push data for car out");

        vehicleLogRedisService.log("access-record-out:" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), carOutKeTopDTO);
        vehicleDataPushKeTopRedisService.insertAccessRecordForOut(carOutKeTopDTO);

        Integer projectId = vehicleIntegratedConfigRedisService.queryProjectIdByParkId(String.valueOf(carOutKeTopDTO.getParkId()));
        ProjectBO projectBO = queryProjectService.queryProject(projectId);

        vehicleAlarmBusinessService.sendAlarmForKeTopCarOutExceptionPass(projectId, carOutKeTopDTO.getPassType(), carOutKeTopDTO.getImgInfo(),
                StringUtils.substringAfterLast(carOutKeTopDTO.getLeavePlace(), "-"));
        vehicleAlarmBusinessService.sendAlarmForKeTopBlacklist(projectId, carOutKeTopDTO.getPlateNo(), carOutKeTopDTO.getImgInfo());

        vehicleDataPushBusinessService.keTopCarOut(carOutKeTopDTO, projectBO);

        log.info("end: ke-top push data for car out");
        return ResponseCommonPushKeTopDTO.success();
    }

    @PostMapping("/PostPayFeeInfo")
    public ResponseCommonPushKeTopDTO carPay(@RequestBody CarPayKeTopDTO carPayKeTopDTO) {
        log.info("start: ke-top push data for car pay");

        vehicleLogRedisService.log("pay-record:" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), carPayKeTopDTO);
        vehicleDataPushKeTopRedisService.insertPayRecord(carPayKeTopDTO);

        Integer projectId = vehicleIntegratedConfigRedisService.queryProjectIdByParkId(String.valueOf(carPayKeTopDTO.getParkId()));
        vehicleDataPushBusinessService.keTopCarPay(carPayKeTopDTO, projectId);

        log.info("end: ke-top push data for car pay");
        return ResponseCommonPushKeTopDTO.success();
    }

}
